import { useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { Dispatch } from "redux";
//封装一个自定义 简化 弹出alert的操作
// 封装一个 自定义hook 简化 弹出alert的操作

function useToast() {
  const dispatch = useDispatch<Dispatch<IToastAction>>();
  return (type: ToastType, message: string) => {
    dispatch({
      type: "Toast_Show",
      payload: {
        type,
        message,
      },
    });
  };
}

// 把 弹出再封装一层，简化为 弹出 成功 / 失败 / ...

function useToastSuccess() {
  let toast = useToast();
  return (message: string) => {
    toast("success", message);
  };
}
function useToastWarning() {
  let toast = useToast();
  return (message: string) => {
    toast("warning", message);
  };
}
function useToastInfo() {
  let toast = useToast();
  return (message: string) => {
    toast("info", message);
  };
}
function useToastError() {
  let toast = useToast();
  return (message: string) => {
    toast("error", message);
  };
}
export {
  useToast,
  useToastSuccess,
  useToastWarning,
  useToastInfo,
  useToastError,
};
